package com.wh.wisdomsite.xbox.st.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.st.dao.MaterialMapper;
import com.wh.wisdomsite.xbox.st.entity.Material;
import com.wh.wisdomsite.xbox.st.service.IMaterialService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 物料serviceImpl
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.st.service.impl
 * @date 2018年12月24日 14:42
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Service("materialService")
@Transactional(rollbackFor = {Exception.class, RuntimeException.class})
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    private static final Log logger = LogFactory.getLog(MaterialServiceImpl.class);

    @Autowired
    private MaterialMapper materialMapper;

    @Override
    public String getMaId(Material material) {
        try {
            Material material1 = getMaterial(material);
            //判断有无
            if(material1 == null){
                //若无，添加，返回ID
                material.setMaId(UUID.randomUUID().toString().replace("-", ""));
                materialMapper.insert(material);
                return material.getMaId();
            }else {
                //若有，直接返回ID
                return material1.getMaId();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Material getMaterial(Material material) {
        //查询是否有此物料
        Material material1 = baseMapper.selectOne(material);
        return material1;
    }

    @Override
    public List<Map<String, Object>> getMapsByConditon(Wrapper<Material> var1) {
        return materialMapper.getMapsByConditon(var1);
    }
}
